package br.com.lumis.ims.service.cloud.dataprovider;

import java.util.Iterator;
import java.util.List;

import lumis.doui.source.ISourceData;
import lumis.doui.source.TabularData;
import lumis.doui.table.TableDataProvider;
import lumis.doui.table.TableSource;
import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.util.ITransaction;
import br.com.lumis.ims.model.Disco;
import br.com.lumis.ims.model.Instancia;
import br.com.lumis.ims.service.EC2Service;
import br.com.lumis.ims.service.cloud.util.ImsRequestParameters;

import com.amazonaws.auth.AWSCredentials;

public class DiscoDataProvider extends TableDataProvider {

	public DiscoDataProvider() throws PortalException {
		super();
	}

	@Override
	public void loadData(SessionConfig sessionConfig, TableSource source,
			ITransaction transaction) throws PortalException {

		AWSCredentials credentials = ImsRequestParameters.getCredentials( sessionConfig,  transaction);
		TabularData tabularData = source.getData();

		String idInstancia = (String) source.getDouiContext().getRequest()
				.getParameter(ImsRequestParameters.INSTANCIAID);

		List<Instancia> instancias = new EC2Service()
				.getServidores(credentials);
		Iterator<Instancia> itInstancias = instancias.iterator();

		while (itInstancias.hasNext()) {
			Instancia instancia = itInstancias.next();
			if (instancia.getId().equals(idInstancia)) {

				Iterator<Disco> itDiscos = instancia.getDiscos().iterator();
				while (itDiscos.hasNext()) {
					ISourceData sourceData = tabularData.addRow();

					Disco disco = itDiscos.next();
					//System.out.println(disco.getDevice() + "\t" + disco.getId());
					sourceData.put(ImsRequestParameters.ID, disco.getId());
					sourceData.put(ImsRequestParameters.DEVICE, disco
							.getDevice());
					sourceData.put(ImsRequestParameters.INSTANCIAID, instancia.getId());
				}
			}
		}

	}


}
